package cn.itcast.controller;

import cn.itcast.constant.MessageConstant;
import cn.itcast.entity.Result;

import cn.itcast.pojo.Setmeal;
import cn.itcast.service.CheckGroupService;
import cn.itcast.service.CheckItemService;
import cn.itcast.service.MobileSetmealCheckgroupService;
import cn.itcast.service.SetmealService;
import com.alibaba.dubbo.config.annotation.Reference;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;
@CrossOrigin
@RestController
@RequestMapping(path = "/setmeal")
@Slf4j
public class SetMealController {
    @Reference
    private SetmealService setmealService;

    @Reference
    private CheckGroupService checkGroupService;

    @Reference
    private CheckItemService checkItemService;

    @Reference
    private MobileSetmealCheckgroupService mobileSetmealCheckgroupService;

    @GetMapping(path = "/findAll")
    @CrossOrigin
    public Result querySetMealList() {
        List<Setmeal> setmealList = this.setmealService.findAll();

        if (ObjectUtils.isEmpty(setmealList)) {
            return new Result(false, MessageConstant.QUERY_SETMEALLIST_FAIL, null);
        }

        return new Result(true, MessageConstant.QUERY_SETMEALLIST_SUCCESS, setmealList);
    }

    /**
     * 查询单个套餐的详情
     * 分析：
     * 实体类setmeal中，包含了checkgroup的信息，需要通过setmeal的id
     * 查询到对应的checkgroup的id，再根据这个checkgroup的id，查询到对应的checkitem的id，
     * 最终封装成一个setmeal对象进行返回
     *
     * @param id 套餐的id
     * @return
     */
    @GetMapping(path = "/{id}")
    @CrossOrigin
    public Result querySetMealDetail(@PathVariable("id") String id) {
        try {
            Setmeal setmeal = this.mobileSetmealCheckgroupService.querySetmeal(Integer.valueOf(id));
            return new Result(true, MessageConstant.QUERY_SETMEAL_SUCCESS,setmeal);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_CHECKITEM_FAIL);
        }
    }
}
